package coml.jiyun.hp.xaizai.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.xaizai.R;
import coml.jiyun.hp.xaizai.adapter.HomeAdapter;
import coml.jiyun.hp.xaizai.bean.WanBean;
import coml.jiyun.hp.xaizai.model.ImpWanModel;
import coml.jiyun.hp.xaizai.presenter.ImpWanPresenter;
import coml.jiyun.hp.xaizai.view.WanView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements WanView{

    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private HomeAdapter homeAdapter;
    private RecyclerView mRlv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        ImpWanPresenter impWanPresenter = new ImpWanPresenter(this, new ImpWanModel());
        impWanPresenter.getData();
    }

    private void initView(View inflate) {

        mRlv = (RecyclerView) inflate.findViewById(R.id.rlv);

        datasBeans = new ArrayList<>();
        homeAdapter = new HomeAdapter(getActivity(), datasBeans);
        mRlv.setAdapter(homeAdapter);
        mRlv.setLayoutManager(new LinearLayoutManager(getActivity()));

        homeAdapter.setItemOnick(new HomeAdapter.itemOnick() {
            @Override
            public void log(int lo) {
                //弹出DiaLog
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("是否要删除这条数据");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        homeAdapter.delete(lo);
                    }
                });
                builder.setNegativeButton("取消",null);
                builder.show();
            }
        });
    }

    @Override
    public void onSuccess(WanBean wanBean) {
        List<WanBean.DataBean.DatasBean> datas = wanBean.getData().getDatas();
        datasBeans.addAll(datas);
        homeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }
}